package com.neusoft.neumooc.dao.front;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.neusoft.core.common.BaseDAO;
import com.neusoft.core.common.exception.DAOException;
import com.neusoft.neumooc.vo.ColumnsVO;
import com.neusoft.neumooc.vo.ContentVO;
import com.neusoft.neumooc.vo.MenuVO;
import com.neusoft.neumooc.vo.TemplatesVO;

@Repository
public class ContentDAOImpl extends BaseDAO implements ContentDAO {

	/*
	 * 左边列表数据
	 */

	private final static String GET_COLUMNSVO_SQL = "SELECT * FROM cw_columns where typeId=?";

	@Override
	public List<ColumnsVO> getColumnsVo(String typeId) throws DAOException {
		return this
				.queryForBeanList(GET_COLUMNSVO_SQL, ColumnsVO.class, typeId);
	}

	private final static String Get_Templatesvo_SQL = "SELECT * FROM cw_templates";

	@Override
	public List<TemplatesVO> getTemplatesVo() throws DAOException {
		return this.queryForBeanList(Get_Templatesvo_SQL, TemplatesVO.class);
	}

	private final static String Get_Contentvo_SQL = "SELECT * FROM cw_contents where columnid=? and status='1'";

	@Override
	public List<ContentVO> getContentVo(String columnsId, int pageIndex,
			int pageSize) throws DAOException {
		return this.getPageData(Get_Contentvo_SQL, ContentVO.class, pageIndex,
				pageSize, columnsId);
	}

	private final static String Get_ColumnidByColumnname_SQL = "select columnId from cw_columns where columnname=?";

	@Override
	public String getColumnIdByColumnName(String columnName)
			throws DAOException {
		return this.queryString(Get_ColumnidByColumnname_SQL, columnName);
	}

	private final static String Get_TypeidByColumnid_SQL = "SELECT typeId FROM cw_columns where columnid=?";

	@Override
	public String getTypeIdByColumnId(String columnId) throws DAOException {
		return this.queryString(Get_TypeidByColumnid_SQL, columnId);
	}

	private final static String Get_ListtplfileByColumnid_SQL = "SELECT listtplfile FROM cw_columns where columnid=?";

	@Override
	public String getListTplFileByColumnId(String columnId) throws DAOException {
		return this.queryString(Get_ListtplfileByColumnid_SQL, columnId);
	}

	private final static String Get_PagetplfileByColumnid_SQL = "SELECT pagetplfile FROM cw_columns where columnid=?";

	@Override
	public String getPageTplFileByColumnId(String columnId) throws DAOException {
		return this.queryString(Get_PagetplfileByColumnid_SQL, columnId);
	}

	private final static String Get_getContentvoNum_SQL = "SELECT count(1) FROM cw_contents where columnid=? and status='1'";

	@Override
	public int getContentVoNum(String columnId) throws DAOException {
		return this.queryInteger(Get_getContentvoNum_SQL, columnId);
	}

	private final static String Get_ContentDetail_SQL = "SELECT * FROM cw_contents where contentid=?";

	@Override
	public List<ContentVO> getContentDetail(String contentid)
			throws DAOException {
		return this.queryForBeanList(Get_ContentDetail_SQL, ContentVO.class,
				contentid);
	}

	@Override
	public List<ContentVO> getSearchInfo(String search, int pageIndex,
			int pageSize) throws DAOException {
		String Get_SearchInfo_SQL = "SELECT * FROM cw_contents where status='1'";
		Get_SearchInfo_SQL += " and title like '%" + search + "%'";
		Get_SearchInfo_SQL += " or description like '%" + search + "%'";
		Get_SearchInfo_SQL += " or text like '%" + search + "%'";
		return this.getPageData(Get_SearchInfo_SQL, ContentVO.class, pageIndex,
				pageSize);
	}

	@Override
	public int getSearchInfoNum(String search) throws DAOException {
		String Get_SearchInfo_SQL = "SELECT count(1) FROM cw_contents where status='1'";
		Get_SearchInfo_SQL += " and title like '%" + search + "%'";
		Get_SearchInfo_SQL += " or description like '%" + search + "%'";
		Get_SearchInfo_SQL += " or text like '%" + search + "%'";
		return this.queryForInteger(Get_SearchInfo_SQL);
	}

	private final static String GET_LISTTPLFILEBYTYPEID_SQL = "select listTplFile from cw_columns where typeid=? group by listTplFile";

	@Override
	public String getListTplFileByTypeId(String typeId) throws DAOException {
		return this.queryString(GET_LISTTPLFILEBYTYPEID_SQL, typeId);
	}

	private final static String GET_PAGETPLFILEBYTYPEID_SQL = "select pagetplfile from cw_columns where typeid=? group by pagetplfile;";

	@Override
	public String getPageTplFileByTypeId(String typeId) throws DAOException {
		return this.queryString(GET_PAGETPLFILEBYTYPEID_SQL, typeId);
	}

	private final static String GET_COLUMN_NAME_BY_COLUMN_ID_SQL = "select columnName from cw_columns where columnId=?";

	@Override
	public String getColumnNameByColumnId(String columnId) throws DAOException {
		return this.queryString(GET_COLUMN_NAME_BY_COLUMN_ID_SQL, columnId);
	}

	private final static String GET_MENU_NAME_BY_COLUMN_ID_SQL = "SELECT * FROM neu_coweb.cw_menus where menuId=(SELECT parentId FROM cw_menus where link=?)";

	@Override
	public MenuVO getMenuNameByColumnId(String columnId) throws DAOException {
		return this.queryForBean(GET_MENU_NAME_BY_COLUMN_ID_SQL, MenuVO.class,
				columnId);
	}

	private final static String GET_MENU_NAME_BY_US_COLUMN_ID_SQL = "SELECT menuNameUs FROM cw_menus where link=?";

	@Override
	public String getMenuNameUsByColumnId(String columnId) throws DAOException {
		return this.queryString(GET_MENU_NAME_BY_US_COLUMN_ID_SQL, columnId);
	}

	private final static String GET_TITLE_BY_CONTENT_ID_SQL = "SELECT title FROM cw_contents where contentId =?";

	@Override
	public String getTitleByContentId(String contentId) throws DAOException {

		return this.queryString(GET_TITLE_BY_CONTENT_ID_SQL, contentId);
	}

	private final static String GET_COLUMN_ID_BY_CONTENT_ID_SQL = "SELECT columnId FROM cw_contents where contentId =?";

	@Override
	public String getColumnIdByContentId(String contentId) throws DAOException {
		return this.queryString(GET_COLUMN_ID_BY_CONTENT_ID_SQL, contentId);
	}
}
